package com.yunfei.utils.http;

import com.google.gson.Gson;
import com.yunfei.utils.TokenUtil;
import com.yunfei.utils.file.FileUtils;
import com.yunfei.utils.file.InputStreamToFile;
import com.yunfei.utils.string.StringUtil;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.nio.charset.Charset;

public class DownLoadContractHttpUtil {
    private static CloseableHttpClient httpClient;

    static {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
        cm.setDefaultMaxPerRoute(20);
        cm.setDefaultMaxPerRoute(50);
        httpClient = HttpClients.custom().setConnectionManager(cm).build();
    }

    public static JSONObject get(String url,String token) {
        CloseableHttpResponse response = null;
        BufferedReader in = null;
        JSONObject result = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
            httpGet.setConfig(requestConfig);
            httpGet.setConfig(requestConfig);
            httpGet.addHeader("Content-type", "application/json; charset=utf-8");
            httpGet.setHeader("Accept", "application/json");
            if(!StringUtil.isNullOrEmpty(token)){
                httpGet.setHeader("token",token);
            }
            response = httpClient.execute(httpGet);
            String jsonResult = EntityUtils.toString(response.getEntity());// 返回json格式：
            result = JSONObject.fromObject(jsonResult);
           /* in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            result = sb.toString();*/
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != response) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static JSONObject post(String token,String url, String jsonString,String savePath,String fileName) {
        CloseableHttpResponse response = null;
        BufferedReader in = null;
        JSONObject jsonObject=null;
        try {
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
            httpPost.setConfig(requestConfig);
            httpPost.setConfig(requestConfig);
            httpPost.addHeader("Content-type", "application/json; charset=utf-8");
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("token", token);
            httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
            response = httpClient.execute(httpPost);
            HttpEntity httpEntity=response.getEntity();
            InputStream  inputStream=httpEntity.getContent();
            File file=new File(savePath);
            if(!file.exists()){
                FileUtils.mkdir(savePath);
            }
            //InputStreamToFile.writeToLocal("D:/image/contract/a.pdf",inputStream);
            InputStreamToFile.writeToLocal(savePath+fileName,inputStream);
            Gson gson=new Gson();
            String jsonResult = EntityUtils.toString(httpEntity);// 返回json格式：
           // String jsonResult = gson.toJson(httpEntity);
            if(!jsonResult.contains("200")){
                jsonObject=JSONObject.fromObject(jsonResult);
                return jsonObject;
            }
            jsonObject=JSONObject.fromObject(jsonResult);
        } catch (IOException e) {
            //e.printStackTrace();
        } finally {
            try {
                if (null != response) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }
public static void main(String args[]){

}
}
